<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"fengxun2017.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.13.2","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="GNU linker ld (GNU Binutils) version 2.42 —— Linker Scripts：PHDRS Command">
<meta property="og:type" content="article">
<meta property="og:title" content="GNU linker ld —— PHDRS Command">
<meta property="og:url" content="http://fengxun2017.github.io/2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/index.html">
<meta property="og:site_name" content="fx">
<meta property="og:description" content="GNU linker ld (GNU Binutils) version 2.42 —— Linker Scripts：PHDRS Command">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-07-08T16:00:00.000Z">
<meta property="article:modified_time" content="2024-07-10T15:45:50.739Z">
<meta property="article:author" content="feng xun">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://fengxun2017.github.io/2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://fengxun2017.github.io/2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/","path":"2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/","title":"GNU linker ld —— PHDRS Command"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>GNU linker ld —— PHDRS Command | fx</title>
  






  <script async defer data-website-id="" src=""></script>

  <script defer data-domain="" src=""></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
  <a target="_blank" rel="noopener" href="https://github.com/fengxun2017" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">fx</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E8%BF%9E%E6%8E%A5%EF%BC%9A"><span class="nav-number">1.</span> <span class="nav-text">参考连接：</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">feng xun</p>
  <div class="site-description" itemprop="description">FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://fengxun2017.github.io/2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="feng xun">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="fx">
      <meta itemprop="description" content="FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="GNU linker ld —— PHDRS Command | fx">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          GNU linker ld —— PHDRS Command
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-07-09 00:00:00" itemprop="dateCreated datePublished" datetime="2024-07-09T00:00:00+08:00">2024-07-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/GUN-linker/" itemprop="url" rel="index"><span itemprop="name">GUN-linker</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <center>
GNU linker ld (GNU Binutils) version 2.42 —— Linker Scripts：PHDRS Command
</center>

<span id="more"></span>

<hr>
<p><strong>个人笔记，仅供参考！！</strong></p>
<p><strong>PHDRS</strong> 命令在链接脚本中的作用和目的是精确指定程序头（program headers）。</p>
<p>ELF（Executable and Linkable Format，可执行与可链接格式）的目标文件格式使用程序头（也称为段头）来描述如何将程序加载到内存中。可以使用 objdump 程序的 -p 选项来打印这些程序头。</p>
<p>程序头是一组数据结构，它们以二进制格式存储在可执行文件中。在 ELF 文件中，它们通常位于文件头之后，紧接着是各个段的数据。每个段对应于可执行文件中的一部分数据，例如代码、只读数据、只写数据等。而程序头告诉系统加载器如何将这些段加载到内存中。启动可执行程序时，系统加载器会读取程序头，以确定如何加载程序。<br>每个程序头具有以下字段：</p>
<ul>
<li>p_type：指示段的类型（例如加载段、动态段等）。</li>
<li>p_offset：指示段在文件中的偏移量。</li>
<li>p_vaddr：指示段在内存中的虚拟地址。</li>
<li>p_filesz：指示段在文件中的大小。</li>
<li>p_memsz：指示段在内存中的大小。</li>
<li>p_flags：指示段的标志（例如可读、可写、可执行等）。</li>
</ul>
<p>链接器默认会创建合理的程序头。但在某些情况下，可能需要更精确地指定程序头。可以使用 <strong>PHDRS</strong> 命令来实现这一目的。当链接器在链接脚本中看到 <strong>PHDRS</strong> 命令时，它将仅创建指定的程序头，而不会创建其他程序头。</p>
<p>链接器只在生成 ELF 输出文件时关注 PHDRS 命令。在其他情况下，链接器将简单地忽略 PHDRS。</p>
<p> <strong>PHDRS</strong> 命令的语法如下。其中 PHDRS、FILEHDR、AT 和 FLAGS 是关键字。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PHDRS</span><br><span class="line">&#123;</span><br><span class="line">  name type [ FILEHDR ] [ PHDRS ] [ AT ( address ) ]</span><br><span class="line">        [ FLAGS ( flags ) ] ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>name</code> 仅用于链接脚本中的 SECTIONS 命令的引用。它不会放入输出文件中。程序头名称存储在单独的命名空间中，不会与符号名称、文件名或节名称冲突。每个程序头必须具有唯一的名称。这些头按顺序处理，通常会映射到按升序加载地址排列的段。</p>
<p><code>type</code> 可以是以下之一。数字表示关键字的值。</p>
<ul>
<li>PT_NULL（0）：表示未使用的程序头。</li>
<li>PT_LOAD（1）：表示此程序头描述要从文件中加载的段。</li>
<li>PT_DYNAMIC（2）：表示包含动态链接信息的段。</li>
<li>PT_INTERP（3）：表示包含程序解释器名称的段。</li>
<li>PT_NOTE（4）：表示包含注释信息的段。</li>
<li>PT_SHLIB（5）：保留的程序头类型，由 ELF ABI 定义但未指定。</li>
<li>PT_PHDR（6）：表示包含程序头的段。</li>
<li>PT_TLS（7）：指示包含线程本地存储的段。</li>
<li>expression ：给出程序头的数字类型的表达式。这可以用于上面没有定义的类型。</li>
</ul>
<p>某些程序头类型描述了系统加载器将从文件中加载的内存段（segments）。在链接脚本中，通过将可分配的输出段（sections）放置在这些段（segments）中来指定这些段的内容。可以使用输出段属性“:phdr”将输出段放置在特定段（segment）中。</p>
<p>允许将某些输出段放置在多个段（segement）中。这仅意味着一个内存段(segment )包含另一个内存段(segment)。可以重复使用 “:phdr”，每个应包含该输出段的段（segment）使用一次。</p>
<p>如果使用 “:phdr” 将输出段放置在一个或多个段（segment）中，那么链接器将在相同的段（segment）中放置所有后续的可分配输出段，这是为了方便起见，因为通常会将一整组连续的输出段放置在单个段（segment）中。可以使用 “:NONE” 来覆盖默认段，并告诉链接器不要将输出段节放入任何段（segment）中。</p>
<p>可以在程序头类型之后使用 <strong>FILEHDR</strong> 和 <strong>PHDRS</strong> 关键字进一步描述段的内容。FILEHDR 关键字表示该段应包含 ELF 文件头。PHDRS 关键字表示该段应包含 ELF 程序头本身。如果应用于可加载段（type&#x3D;PT_LOAD），则所有先前的可加载段必须具有这些关键字之一。</p>
<p><code>AT ( address )</code>：可以通过使用 AT 表达式来指定内存中的特定地址来加载段（segment）。这与输出段属性的中的 AT 属性作用相同。程序头的 AT 命令会覆盖输出段属性。</p>
<p><code>FLAGS ( flags )</code>：链接器通常会基于组成段(segment)的各个输出段来设置段标志。可以使用 FLAGS 关键字显式指定段标志。flags 的值必须是整数。它用于设置程序头中的 p_flags 字段。</p>
<p>一个使用 <strong>PHDRS</strong> 命令的示例。展示了在ELF系统上使用的典型程序头集合。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PHDRS</span><br><span class="line">&#123;</span><br><span class="line">  headers PT_PHDR PHDRS ;</span><br><span class="line">  interp PT_INTERP ;</span><br><span class="line">  text PT_LOAD FILEHDR PHDRS ;</span><br><span class="line">  data PT_LOAD ;</span><br><span class="line">  dynamic PT_DYNAMIC ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>以下是链接脚本的示例。显示了在内存中使用的一组典型程序头。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">SECTIONS</span><br><span class="line">&#123;</span><br><span class="line">  . = SIZEOF_HEADERS;</span><br><span class="line">  .interp : &#123; *(.interp) &#125; :text :interp</span><br><span class="line">  .text : &#123; *(.text) &#125; :text</span><br><span class="line">  .rodata : &#123; *(.rodata) &#125; /* 默认为 :text */</span><br><span class="line">  …</span><br><span class="line">  . = . + 0x1000; /* 移动到内存中的新页面 */</span><br><span class="line">  .data : &#123; *(.data) &#125; :data</span><br><span class="line">  .dynamic : &#123; *(.dynamic) &#125; :data :dynamic</span><br><span class="line">  …</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="参考连接："><a href="#参考连接：" class="headerlink" title="参考连接："></a>参考连接：</h4><p>【1】<a target="_blank" rel="noopener" href="https://sourceware.org/binutils/docs/ld/PHDRS.html">https://sourceware.org/binutils/docs/ld/PHDRS.html</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>feng xun
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://fengxun2017.github.io/2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/" title="GNU linker ld —— PHDRS Command">http://fengxun2017.github.io/2024/07/09/GUN-linker-Linker-Scripts-PHDRS-Command/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/07/07/GUN-linker-Linker-Scripts-MEMORY-Command/" rel="prev" title="GNU linker ld —— MEMORY Command">
                  <i class="fa fa-chevron-left"></i> GNU linker ld —— MEMORY Command
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/07/12/GUN-linker-Linker-Scripts-Output-Section-Description/" rel="next" title="GNU linker ld —— Output Section Description">
                  GNU linker ld —— Output Section Description <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">feng xun</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">552k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">11:30</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
